﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fiddler;
using V8.Net;
using System.CodeDom.Compiler;
using System.CodeDom;
using System.IO;
using System.Resources;
using ProgramCat.Properties;
using ScintillaNET;
using ProgramCat.FiddlerExtension.Core.Utils;
using ProgramCat.FiddlerExtension.Core.JsEditor;

namespace ProgramCat.FiddlerExtension
{
    public partial class PropertiesCatControl : UserControl
    {
        //js引擎的编辑框
        JsEditor jsEngineEditor;
        //jshook的编辑框
        JsEditor jsHookEditor;
        /// <summary>
        /// 初始化
        /// </summary>
        public PropertiesCatControl()
        {
            Config.Instance.Init();
            //初始化系统生成组件
            InitializeComponent();
            webBrowser_index.Navigate(Config.Instance.GetIndexUrl());
            //初始化下拉菜单
            InitComboBox();
            //初始化所有JS编辑框
            InitJsEditors();
            //初始化V8引擎
            InitV8Engine();
            //初始化jshook
            InitJsHook();
        }
        private void InitComboBox()
        {
            comboBox_GenerationMode.SelectedIndex = 0;
            comboBox_CompilingMode.SelectedIndex = 0;
            comboBox_sha.SelectedIndex = 0;
            comboBox_AesMode.SelectedIndex = 0;
            comboBox_AesPadding.SelectedIndex = 0;
            comboBox_AesKey.SelectedIndex = 0;
            comboBox_AesIv.SelectedIndex = 0;
            comboBox_AesOut.SelectedIndex = 0;
            comboBox_DesMode.SelectedIndex = 0;
            comboBox_DesPadding.SelectedIndex = 0;
            comboBox_DesKey.SelectedIndex = 0;
            comboBox_DesIV.SelectedIndex = 0;
            comboBox_DesOut.SelectedIndex = 0;
            comboBox_3DesMode.SelectedIndex = 0;
            comboBox_3DesPadding.SelectedIndex = 0;
            comboBox_3DesKey.SelectedIndex = 0;
            comboBox_3DesIV.SelectedIndex = 0;
            comboBox_3DesOut.SelectedIndex = 0;
        }
        private void InitV8Engine()
        {
            v8 = new V8Engine();
            if (v8 != null)
            {
                TextBox_cmd.Text += "-->V8引擎初始化成功！\r\n";
            }
        }
        private void InitJsEditors()
        {
            jsEngineEditor =  new JsEditor(TextPanel_JsCode);
            jsHookEditor = new JsEditor(panel_jshook);
            jsHookEditor.scintilla.Text = Resources.jsHookHelperText;
            jsHookEditor.scintilla.TextChanged += jsHookEditor_TextChanged;
        }

       

        private void InitJsHook()
        {
            JsHookMgr.Instance.jsHookCode = jsHookEditor.scintilla.Text;
            JsHookMgr.Instance.urlPath = textBox_hookurl.Text;
        }
        #region 生成易代码
        Session session = null;

        private void TextBox1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = (e.Data.GetDataPresent("Fiddler.Session[]") ? DragDropEffects.Copy : DragDropEffects.None);


            this.BackColor = Color.FromArgb(0, 255, 0);
        }

        private void TextBox1_DragLeave(object sender, EventArgs e)
        {
            this.BackColor = SystemColors.Control;
        }

        private void TextBox1_DragDrop(object sender, DragEventArgs e)
        {
            this.BackColor = SystemColors.Control;
            Session[] array = (Session[])e.Data.GetData("Fiddler.Session[]");
            if (array == null || array.Length < 1)
                return;
            session = array[0];
            GenerateCode();
        }

        private void ComboBox_CompilingMode_SelectionChangeCommitted(object sender, EventArgs e)
        {
            GenerateCode();
        }

        private void Button_GenerationCode_Click(object sender, EventArgs e)
        {
            GenerateCode();
        }

        private void GenerateCode()
        {
            if (session == null)
            {
                return;
            }
            textBox_eCode.Text = GenerateCodeMgr.GenerateCode(comboBox_GenerationMode.SelectedIndex,
                comboBox_CompilingMode.SelectedIndex,
                session);
        }

        private void ComboBox_GenerationMode_SelectionChangeCommitted(object sender, EventArgs e)
        {
            GenerateCode();
        }

        private void Button_CopyCode_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(textBox_eCode.Text, true);
        }
        #endregion

        #region JS调试工具
        /// <summary>
        /// V8引擎
        /// </summary>
        V8Engine v8 = null;
        private void WebBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            // 防止弹窗
            e.Cancel = true;
            // 获取点击的链接地址
            //string clickUrl = webBrowser1.StatusText;
            // 指向点击的地址
            //webBrowser1.Url = new Uri(clickUrl);
        }
        private void Button_LoadingCode_Click(object sender, EventArgs e)
        {
            //TextBox_cmd.Text = "";
            //重置V8引擎
            v8 = new V8Engine();
            //执行代码
            if (v8 != null)
            {
                TextBox_cmd.Text += "-->JS调试工具启动成功\r\n";
            }
            InternalHandle internalHandle = v8.Execute(jsEngineEditor.scintilla.Text);
            if (internalHandle.IsError)
            {
                //加载出错
                TextBox_cmd.Text += "-->Js加载出错!\r\n";
                V8Analysis v8Analysis = V8Help.Instance.Analysis(internalHandle.Value.ToString());
                if (v8Analysis.line != -1)
                {
                    Line line = jsEngineEditor.scintilla.Lines[v8Analysis.line - 1];
                    jsEngineEditor.scintilla.SetSelection(line.Position + line.Length - 1, line.Position);
                    jsEngineEditor.scintilla.ScrollCaret();
                }
                TextBox_cmd.Text += v8Analysis.data + "\r\n\r\n";
            }
            else
            {
                TextBox_cmd.Text += "-->Js加载成功!\r\n\r\n";
                //刷新函数名列表
                ComboBox_FuncListRefresh();
            }
        }
        bool userSelectedIndex_ComboBox_FuncList = true;
        private void ComboBox_FuncListRefresh()
        {
            ComboBox_FuncList.Items.Clear();
            string[] aaa = v8.GlobalObject.GetPropertyNames();
            if (aaa.Length != 0)
            {
                ComboBox_FuncList.Items.AddRange(aaa);
                userSelectedIndex_ComboBox_FuncList = false;
                ComboBox_FuncList.SelectedIndex = 0;
                //TextBox_Run.Text =string.Format("{0}()", aaa[0]);
            }
        }
        private void ComboBox_FuncList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (userSelectedIndex_ComboBox_FuncList)
            {
                int selectedIndex = ComboBox_FuncList.SelectedIndex;
                if (selectedIndex == -1)
                {
                    return;
                }
                TextBox_Run.Text = string.Format("{0}()", ComboBox_FuncList.Items[selectedIndex].ToString());
            }
            else
            {
                userSelectedIndex_ComboBox_FuncList = true;
            }
        }
        private void Button_Run_Click(object sender, EventArgs e)
        {
            if (TextBox_Run.Text.Trim(' ') == "")
            {
                return;
            }
            InternalHandle internalHandle = v8.Execute(TextBox_Run.Text);
            if (internalHandle.IsError)
            {
                TextBox_cmd.Text += "-->Js运行出错!\r\n";
                V8Analysis v8Analysis = V8Help.Instance.Analysis(internalHandle.Value.ToString());
                if (v8Analysis.line != -1)
                {
                    Line line = jsEngineEditor.scintilla.Lines[v8Analysis.line - 1];
                    jsEngineEditor.scintilla.SetSelection(line.Position + line.Length - 1, line.Position);
                    jsEngineEditor.scintilla.ScrollCaret();
                }
                TextBox_cmd.Text += v8Analysis.data + "\r\n\r\n";
            }
            else
            {
                TextBox_cmd.Text += "-->Js运行成功!\r\n";
                V8Help.Instance.Analysis(internalHandle.Value.ToString());
                TextBox_cmd.Text += String.Format("-->输出:{0}\r\n\r\n", internalHandle.Value.ToString());
            }
        }
        private void Button_Format_Click(object sender, EventArgs e)
        {
            V8Engine v8_temp = new V8Engine();
            v8_temp.Execute(Resources.JsBeautify);
            Handle js_source_text = v8_temp.CreateValue(jsEngineEditor.scintilla.Text);
            Handle options = v8_temp.CreateValue("4");
            InternalHandle text = v8_temp.GlobalObject.Call("js_beautify", null, js_source_text, options);
            jsEngineEditor.scintilla.Text = text.ToString().Replace("\n", "\r\n");
            v8_temp = null;
        }
        private void TextBox_cmd_TextChanged(object sender, EventArgs e)
        {
            TextBox_cmd.Select(TextBox_cmd.Text.Length, 0);
            TextBox_cmd.ScrollToCaret();
        }
        private void Button_Find_Click(object sender, EventArgs e)
        {

            jsEngineEditor.SearchFind(TextBox_Find, true, true);
        }
        //压缩
        private void button_compress_Click(object sender, EventArgs e)
        {
            V8Engine v8_temp = new V8Engine();
            v8_temp.Execute(Resources.JsBeautify);
            Handle js_source_text = v8_temp.CreateValue(jsEngineEditor.scintilla.Text);
            InternalHandle text = v8_temp.GlobalObject.Call("ysjs", null, js_source_text);
            jsEngineEditor.scintilla.Text = text.ToString().Replace("\n", "\r\n");
            v8_temp = null;
        }

        #endregion

        #region JS加密算法
        #region MD5
        private void Button_Md5Cry_Click(object sender, EventArgs e)
        {
            V8Engine v8_temp = new V8Engine();
            v8_temp.Execute(Resources.JsMD5);
            Handle js_md5 = v8_temp.CreateValue(TextBox_Md5Text.Text);
            InternalHandle text = v8_temp.GlobalObject.Call("MD5", null, js_md5);
            //low
            string md5Text = text.Value.ToString();
            TextBox_32MD5L.Text = md5Text;
            TextBox_32MD5.Text = md5Text.ToUpper();
            if (TextBox_32MD5L.Text.Length == 32)
            {
                TextBox_16MD5L.Text = TextBox_32MD5L.Text.Substring(8, 16);
            }
            if (TextBox_32MD5.Text.Length == 32)
            {
                TextBox_16MD5.Text = TextBox_32MD5.Text.Substring(8, 16);
            }
            v8_temp = null;
        }
        private void button_copyMd5_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(Resources.JsMD5, true);
        }
        private void button_V8Ecode_Click(object sender, EventArgs e)
        {

        }
        #endregion
        #region SHA
        private void comboBox_sha_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox_ShaRefresh();
        }
        private void textBox_ShaRefresh()
        {
            V8Engine v8_temp = new V8Engine();
            Handle arg1 = v8_temp.CreateValue(textBox_ShaText.Text);
            InternalHandle @renturn = null;
            switch (comboBox_sha.SelectedIndex)
            {
                case 0:
                    v8_temp.Execute(Resources.JsSha1);
                    @renturn = v8_temp.GlobalObject.Call("SHA1", null, arg1);
                    break;
                case 1:
                    v8_temp.Execute(Resources.JsSha3);
                    @renturn = v8_temp.GlobalObject.Call("SHA3", null, arg1);
                    break;
                case 2:
                    v8_temp.Execute(Resources.JsSha224);
                    @renturn = v8_temp.GlobalObject.Call("SHA224", null, arg1);
                    break;
                case 3:
                    v8_temp.Execute(Resources.JsSha256);
                    @renturn = v8_temp.GlobalObject.Call("SHA256", null, arg1);
                    break;
                case 4:
                    v8_temp.Execute(Resources.JsSha384);
                    @renturn = v8_temp.GlobalObject.Call("SHA384", null, arg1);
                    break;
                case 5:
                    v8_temp.Execute(Resources.JsSha512);
                    @renturn = v8_temp.GlobalObject.Call("SHA512", null, arg1);
                    break;
                default:
                    break;
            }
            if (@renturn != null)
            {
                textBox_Sha.Text = @renturn.Value.ToString();
            }
            v8_temp = null;
        }
        private void button_shaCry_Click(object sender, EventArgs e)
        {
            textBox_ShaRefresh();
        }


        #endregion
        #region AES
        private void button_AesJa_Click(object sender, EventArgs e)
        {
            textBox_AesOutPutRefresh();
        }
        private void textBox_AesOutPutRefresh(bool encrypt = true)
        {
            V8Engine v8_temp = new V8Engine();
            Handle arg1 = v8_temp.CreateValue(textBox_AesText.Text);
            Handle key = v8_temp.CreateValue(textBox_AesKey.Text);
            Handle iv = v8_temp.CreateValue(textBox_AesIv.Text);
            Handle mode = v8_temp.CreateValue(comboBox_AesMode.SelectedIndex.ToString());
            Handle padding = v8_temp.CreateValue(comboBox_AesPadding.SelectedIndex.ToString());
            Handle hexkey = v8_temp.CreateValue(comboBox_AesKey.SelectedIndex.ToString());
            Handle hexiv = v8_temp.CreateValue(comboBox_AesIv.SelectedIndex.ToString());
            Handle hextext = v8_temp.CreateValue("0");
            Handle hexresult = v8_temp.CreateValue(comboBox_AesOut.SelectedIndex.ToString());
            v8_temp.Execute(Resources.JsAes);
            InternalHandle @renturn = null;
            if (encrypt)
            {
                @renturn = v8_temp.GlobalObject.Call("AESencrypt", null, arg1, key, iv, mode, padding, hexkey, hexiv, hextext, hexresult);
            }
            else
            {
                @renturn = v8_temp.GlobalObject.Call("AESdecrypt", null, arg1, key, iv, mode, padding, hexkey, hexiv, hextext);
            }
            textBox_AesOutPut.Text = @renturn.Value.ToString();
            v8_temp = null;
        }
        private void button_AesJe_Click(object sender, EventArgs e)
        {
            textBox_AesOutPutRefresh(false);
        }
        #endregion
        #region DES

        private void textBox_DesOutPutRefresh(bool encrypt = true)
        {
            V8Engine v8_temp = new V8Engine();
            Handle arg1 = v8_temp.CreateValue(textBox_DesText.Text);
            Handle key = v8_temp.CreateValue(textBox_DesKey.Text);
            Handle iv = v8_temp.CreateValue(textBox_DesIV.Text);
            Handle mode = v8_temp.CreateValue(comboBox_DesMode.SelectedIndex.ToString());
            Handle padding = v8_temp.CreateValue(comboBox_DesPadding.SelectedIndex.ToString());
            Handle hexkey = v8_temp.CreateValue(comboBox_DesKey.SelectedIndex.ToString());
            Handle hexiv = v8_temp.CreateValue(comboBox_DesIV.SelectedIndex.ToString());
            Handle hextext = v8_temp.CreateValue("0");
            Handle hexresult = v8_temp.CreateValue(comboBox_DesOut.SelectedIndex.ToString());
            v8_temp.Execute(Resources.JsDes);
            InternalHandle @renturn = null;
            if (encrypt)
            {
                @renturn = v8_temp.GlobalObject.Call("DESencrypt", null, arg1, key, iv, mode, padding, hexkey, hexiv, hextext, hexresult);
            }
            else
            {
                @renturn = v8_temp.GlobalObject.Call("DESdecrypt", null, arg1, key, iv, mode, padding, hexkey, hexiv, hextext);
            }
            textBox_DesOutPut.Text = @renturn.Value.ToString();

            v8_temp = null;
        }
        private void button_DesJa_Click(object sender, EventArgs e)
        {
            textBox_DesOutPutRefresh();
        }
        private void button_DesJe_Click(object sender, EventArgs e)
        {
            textBox_DesOutPutRefresh(false);
        }
        #endregion
        #region 3DES
        private void textBox_3DesOutPutRefresh(bool encrypt = true)
        {
            V8Engine v8_temp = new V8Engine();
            Handle arg1 = v8_temp.CreateValue(textBox_3DesText.Text);
            Handle key = v8_temp.CreateValue(textBox_3DesKey.Text);
            Handle iv = v8_temp.CreateValue(textBox_3DesIV.Text);
            Handle mode = v8_temp.CreateValue(comboBox_3DesMode.SelectedIndex.ToString());
            Handle padding = v8_temp.CreateValue(comboBox_3DesPadding.SelectedIndex.ToString());
            Handle hexkey = v8_temp.CreateValue(comboBox_3DesKey.SelectedIndex.ToString());
            Handle hexiv = v8_temp.CreateValue(comboBox_3DesIV.SelectedIndex.ToString());
            Handle hextext = v8_temp.CreateValue("0");
            Handle hexresult = v8_temp.CreateValue(comboBox_3DesOut.SelectedIndex.ToString());
            v8_temp.Execute(Resources.Js3Des);
            InternalHandle @renturn = null;
            if (encrypt)
            {
                @renturn = v8_temp.GlobalObject.Call("TripleDESencrypt", null, arg1, key, iv, mode, padding, hexkey, hexiv, hextext, hexresult);
            }
            else
            {
                @renturn = v8_temp.GlobalObject.Call("TripleDESdecrypt", null, arg1, key, iv, mode, padding, hexkey, hexiv, hextext);
            }
            textBox_3DesOutPut.Text = @renturn.Value.ToString();

            v8_temp = null;
        }
        private void button_3DesJa_Click(object sender, EventArgs e)
        {
            textBox_3DesOutPutRefresh();
        }
        private void button_3DesJe_Click(object sender, EventArgs e)
        {
            textBox_3DesOutPutRefresh(false);
        }
        #endregion

        #endregion

        #region JsHOOK

       
        private void jsHookEditor_TextChanged(object sender, EventArgs e)
        {
            JsHookMgr.Instance.jsHookCode = jsHookEditor.scintilla.Text;
        }
        private void checkBox_hook_CheckedChanged(object sender, EventArgs e)
        {
            JsHookMgr.Instance.open = checkBox_hook.Checked;
        }
        private void checkBox_urlPathEx_CheckedChanged(object sender, EventArgs e)
        {
            JsHookMgr.Instance.urlPathEx = checkBox_urlPathEx.Checked;
        }
        private void textBox_hookurl_TextChanged(object sender, EventArgs e)
        {
            JsHookMgr.Instance.urlPath = textBox_hookurl.Text;
        }


        #endregion

        #region Utils
        #endregion

        private void ePlug1_Load(object sender, EventArgs e)
        {
            ePlug1.InitPlug();
        }
    }
}
